package com.yugao.fintech.antelope.codegen.controller;

import com.alibaba.fastjson2.JSON;
import com.yugao.fintech.antelope.codegen.model.entity.DatasourceConfig;
import com.yugao.fintech.antelope.codegen.model.entity.GenerateHistory;
import com.yugao.fintech.antelope.codegen.model.entity.TemplateConfig;
import com.yugao.fintech.antelope.codegen.model.rqrs.GenTableRS;
import com.yugao.fintech.antelope.codegen.model.rqrs.GenerateHistoryRS;
import com.yugao.fintech.antelope.codegen.model.rqrs.GeneratorRQ;
import com.yugao.fintech.antelope.codegen.service.DatasourceConfigService;
import com.yugao.fintech.antelope.codegen.service.GenerateHistoryService;
import com.yugao.fintech.antelope.codegen.service.IGenTableService;
import com.yugao.fintech.antelope.codegen.service.TemplateConfigService;
import com.yugao.fintech.antelope.base.ram.ActionType;
import com.yugao.fintech.antelope.base.ram.RamResource;
import com.yugao.fintech.framework.assistant.utils.StringUtils;
import com.yugao.fintech.framework.core.response.Resp;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Api(tags = "低代码-生成历史")
@RestController
@RequestMapping("genHistory")
@RequiredArgsConstructor
public class GenerateHistoryController {
    private final GenerateHistoryService generateHistoryService;
    private final DatasourceConfigService datasourceConfigService;
    private final IGenTableService genTableService;
    private final TemplateConfigService templateConfigService;

    /**
     * 查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
    @RequestMapping("listHistory")
    @RamResource(action = "lcp:history:query", name = "低代码-查询生成代码历史", type = ActionType.READ)
    public Resp<List<GenerateHistoryRS>> listAll() {
        List<GenerateHistory> generateHistories = generateHistoryService.listAll();
        List<GenerateHistoryRS> generateHistoryRSList = generateHistories.stream()
                .map(generateHistory -> {
                    GenerateHistoryRS generateHistoryRS = new GenerateHistoryRS();
                    GeneratorRQ generatorRQ = JSON.parseObject(generateHistory.getConfigContent(), GeneratorRQ.class);
                    GenTableRS genTableRS = genTableService.getGenTableRespByTableId(generatorRQ.getGenTableId());
                    if (genTableRS == null) {
                        return null;
                    }
                    DatasourceConfig datasourceConfig = datasourceConfigService.getById(genTableRS.getDatasourceId());
                    if (Objects.isNull(datasourceConfig)) {
                        return null;
                    }
                    List<String> templateNames = this.listTemplateNames(StringUtils.toList(generatorRQ.getTemplateIds(), ",", Long.class));
                    generateHistoryRS.setCreatedAt(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(generateHistory.getCreatedAt()));
                    generateHistoryRS.setConfigContent(generatorRQ);
                    generateHistoryRS.setDatasource(datasourceConfig.getName());
                    generateHistoryRS.setTemplateNames(templateNames);
                    return generateHistoryRS;
                })
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
        return Resp.ok(generateHistoryRSList);
    }

    private List<String> listTemplateNames(List<Long> idList) {
        return templateConfigService.listByIds(idList)
                .stream()
                .map(TemplateConfig::getName)
                .collect(Collectors.toList());
    }

}